<template>
  <div class="page-container">

    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="角色名"></el-input>
        </el-form-item>
        <el-form-item>
          <kt-button icon="fa fa-search" :label="$t('action.search')" perms="sys:role:view" type="primary"
                     @click="findPage(null)"/>
        </el-form-item>
        <el-form-item>
          <kt-button icon="fa fa-plus" :label="$t('action.add')" perms="sys:role:add" type="primary"
                     @click="handleAdd"/>
        </el-form-item>
      </el-form>
    </div>

    <!--表格内容栏-->
    <kt-table permsEdit="sys:role:edit" permsDelete="sys:role:delete" :highlightCurrentRow="true" :stripe="false"
              :data="pageResult" :columns="columns" :showBatchDelete="false" @handleCurrentChange="showAssign"
              @findPage="findPage" @handleEdit="handleEdit" @handleDelete="handleDelete">
    </kt-table>

    <!--新增编辑界面-->
    <el-dialog :title="operation ? '新增' : '编辑'" width="40%" :visible.sync="dialogVisible" :close-on-click-modal="false">
      <el-form :model="dataForm" label-width="80px" :rules="dataFormRules" ref="dataForm" :size="size">
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色名" prop="name">
          <el-input v-model="dataForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注 " prop="remark">
          <el-input v-model="dataForm.remark" auto-complete="off" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="size" @click.native="dialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button :size="size" type="primary" @click.native="submitForm" :loading="editLoading">
          {{$t('action.submit')}}
        </el-button>
      </div>
    </el-dialog>

    <!--分配权限-->
    <el-dialog title="分配权限" :visible.sync="assignDialogVisible" :close-on-click-modal="false">
      <assign-permissions :role="role"></assign-permissions>
    </el-dialog>
  </div>
</template>

<script>
  import AssignPermissions from "./components/Role/AssignPermissions"
  import KtTable from "@/views/Core/KtTable"
  import KtButton from "@/views/Core/KtButton"
  import TableTreeColumn from '@/views/Core/TableTreeColumn'
  import {format} from "@/utils/datetime"

  export default {
    components: {
      KtTable,
      KtButton,
      TableTreeColumn,
      AssignPermissions
    },
    data() {
      return {
        size: 'small',
        filters: {
          name: ''
        },
        columns: [
          {prop: "id", label: "ID", minWidth: 50},
          {prop: "name", label: "角色名", minWidth: 120},
          {prop: "remark", label: "备注", minWidth: 120},
          {prop: "createBy", label: "创建人", minWidth: 120},
          {prop: "createTime", label: "创建时间", minWidth: 120, formatter: this.dateFormat}
          // {prop:"lastUpdateBy", label:"更新人", minWidth:100},
          // {prop:"lastUpdateTime", label:"更新时间", minWidth:120, formatter:this.dateFormat}
        ],
        pageRequest: {pageNum: 1, pageSize: 10},
        pageResult: {},

        operation: false, // true:新增, false:编辑
        dialogVisible: false, // 新增编辑界面是否显示
        assignDialogVisible: false,
        editLoading: false,
        dataFormRules: {
          name: [
            {required: true, message: '请输入角色名', trigger: 'blur'}
          ]
        },
        // 新增编辑界面数据
        dataForm: {
          id: 0,
          name: '',
          remark: ''
        },
        selectRole: {},
        menuLoading: false,
        authLoading: false,
        role: ""
      }
    },
    methods: {
      // 获取分页数据
      findPage: function (data) {
        if (data !== null) {
          this.pageRequest = data.pageRequest
        }
        this.pageRequest.columnFilters = {name: {name: 'name', value: this.filters.name}}
        this.$api.role.findPage(this.pageRequest).then((res) => {
          this.pageResult = res.data;
        }).then(data != null ? data.callback : '')
      },
      // 批量删除
      handleDelete: function (data) {
        this.$api.role.batchDelete(data.params).then(data.callback)
      },
      // 显示新增界面
      handleAdd: function () {
        this.dialogVisible = true;
        this.operation = true;
        this.dataForm = {
          id: 0,
          name: '',
          remark: ''
        }
      },
      // 显示编辑界面
      handleEdit: function (params) {
        this.dialogVisible = true;
        this.operation = false;
        this.dataForm = Object.assign({}, params.row)
      },
      // 编辑
      submitForm: function () {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true;
              let params = Object.assign({}, this.dataForm);
              this.$api.role.save(params).then((res) => {
                this.editLoading = false;
                if (res.code === 200) {
                  this.$message({message: '操作成功', type: 'success'});
                  this.dialogVisible = false;
                  this.$refs['dataForm'].resetFields()
                } else {
                  this.$message({message: '操作失败, ' + res.msg, type: 'error'})
                }
                this.findPage(null)
              })
            })
          }
        })
      },
      // 显示角色分配
      showAssign(val) {
        this.role = val;
        this.assignDialogVisible = true
      },
      // 时间格式化
      dateFormat: function (row, column, cellValue, index) {
        return format(row[column.property])
      }

    },
    mounted() {
    }
  }
</script>
<style scoped>
  .menu-container {
    margin-top: 10px;
  }

  .menu-header {
    padding-left: 8px;
    padding-bottom: 5px;
    text-align: left;
    font-size: 16px;
    color: rgb(20, 89, 121);

  }
</style>
